<?php

namespace app\admin\controller\marketing;

use app\admin\controller\Controllers;
use app\admin\service\marketing\Coupons as CouponsService;
use app\admin\model\marketing\Coupons as CouponsModels;

class Coupons extends Controllers
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index( CouponsService $service )
    {
        $get = $this->request->get();
        $this->validate($get, 'app\admin\validate\marketing\Coupons.index');
        $list = $service->indexPaginate($get);
        $config = config('field.coupons');
        $this->assign([
            'get' => $get,
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total(),
            'config' => $config
        ]);
        return $this->fetch('marketing/coupons/index');
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create(  )
    {
        $config = config('field.coupons');
        $this->assign([
            'config' => $config,
        ]);
        return $this->fetch('marketing/coupons/create');
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save( CouponsService $service )
    {
        $post = $this->request->only(['type','start_date','end_date','price'], 'post');
        $this->validate($post, 'app\admin\validate\marketing\Coupons.Create');
        $curriculumid = $service->create($post);
        if ( false === $curriculumid ) {
            return failed('新增失败');
        }
        return success();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit( CouponsModels $model )
    {
        $config = config('field.coupons');
        $this->assign([
            'data' => $model,
            'config' => $config,
        ]);
        return $this->fetch('marketing/coupons/edit');
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(CouponsModels $model, CouponsService $service)
    {
        $put = $this->request->only(['type','start_date','end_date','price'], 'put');
        $this->validate($put, 'app\admin\validate\marketing\Coupons.Update');
        $update = $service->save($model, $put);
        if ( false === $update ) {
            return failed('编辑失败');
        }
        return success();
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(int $id, CouponsService $service)
    {
        $this->validate(['id'=>$id], 'app\admin\validate\marketing\Coupons.delete');
        if ( $service->destory($id) ) {
            return success();
        }
        return failed('删除失败');
    }

    /**
     * 批量删除
     * @param CouponsService $service
     * @return \think\Response
     */
    public function destory(CouponsService $service)
    {
        $delete = $this->request->only(['ids'], 'post');
        $this->validate($delete, 'app\admin\validate\marketing\Coupons.destory');
        if ( $service->destory($delete['ids']) ) {
            return success();
        }
        return failed('删除失败');
    }

}
